package com.boyuan.huijiaoyu;

import java.util.ArrayList;
import java.util.List;
import com.example.contactfragment.ContactService;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;
import com.magicsoft.constant.Constant;

import android.os.Bundle;
import android.os.Handler;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class FindSearchActivity extends Activity implements OnClickListener{

	private ListView listview;
	private ContactService contactService;
	String url_huodong = Constant.BaseUrl + "resource/activity";
	String url_xinwen = Constant.BaseUrl + "resource/article";
	String url_shiping = Constant.BaseUrl + "resource/video";
	String url_zhuanjia = Constant.BaseUrl + "resource/expert";
	private MyAdapter adapter;
	ArrayList<String> titlelist1 = new ArrayList<String>();
	ArrayList<String> contentlist1 = new ArrayList<String>();
	ArrayList<String> titlelist2 = new ArrayList<String>();
	ArrayList<String> contentlist2 = new ArrayList<String>();
	ArrayList<String> titlelist3 = new ArrayList<String>();
	ArrayList<String> contentlist3 = new ArrayList<String>();
	ArrayList<String> titlelist4 = new ArrayList<String>();
	ArrayList<String> contentlist4 = new ArrayList<String>();
	boolean isok1 = false;
	boolean isok2 = false;
	boolean isok3 = false;
	boolean isok4 = false;
	private WebView webview;
	private ImageView imag_huodong;
	private ImageView imag_xinwen;
	private ImageView imag_shiping;
	private ImageView imag_zhuangjia;
	static int a = 1;
	boolean isfirst1 = true;
	boolean isfirst2 = true;
	boolean isfirst3 = true;
	boolean isfirst4 = true;
	private RelativeLayout bofang;
	private TextView tv_hudong;
	private TextView tv_xinwen;
	private TextView tv_shiping;
	private TextView tv_zhuanjia;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_find_search);
		getHuodong();
		getXinwen();
		getShiping();
		getZhuanjia();
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				if(isok1==true&&isok2==true&&isok3==true&&isok4==true){
					a = 1;
					initUI();
					return;
				}else{
					new Handler().postDelayed(this, 1000);
				}
			}
		}, 1000);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if(webview != null){
			webview.reload();
		}
	}
	
	private void initUI() {
		findViewById(R.id.lay_back_find).setOnClickListener(this);
		findViewById(R.id.lay_searhuodong).setOnClickListener(this);
		findViewById(R.id.lay_searxinwen).setOnClickListener(this);
		findViewById(R.id.lay_searshiping).setOnClickListener(this);
		findViewById(R.id.lay_searzhuanjia).setOnClickListener(this);
		tv_hudong = (TextView) findViewById(R.id.tv1_huodong);
		tv_xinwen = (TextView) findViewById(R.id.tv1_xinwen);
		tv_shiping = (TextView) findViewById(R.id.tv1_shiping);
		tv_zhuanjia = (TextView) findViewById(R.id.tv1_zhuanjia);
		imag_huodong = (ImageView) findViewById(R.id.imag_searhuodong);
		imag_xinwen = (ImageView) findViewById(R.id.imag_searxinwen);
		imag_shiping = (ImageView) findViewById(R.id.imag_searshiping);
		imag_zhuangjia = (ImageView) findViewById(R.id.imag_searzhuanjia);
		bofang = (RelativeLayout) findViewById(R.id.bofang);
		listview = (ListView) findViewById(R.id.listView_souyisou);
		webview = (WebView) findViewById(R.id.webView_souyisou);
		initListview();
	}

	private void initListview() {
		adapter = new MyAdapter();
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@SuppressLint({ "SetJavaScriptEnabled", "NewApi", "InlinedApi" })
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				listview.setVisibility(View.GONE);
				webview.setVisibility(View.VISIBLE);
				if(a == 1){
					String data = contentlist1.get(arg2);
					webview.loadDataWithBaseURL(null, data, null, "utf-8", null);
				}else if(a == 2){
					String data = contentlist2.get(arg2);
					webview.loadDataWithBaseURL(null, data, null, "utf-8", null);
				}else if(a ==3){
					String data = contentlist3.get(arg2);
					webview.setWebViewClient(new MyWebViewClient());
					webview.getSettings().setJavaScriptEnabled(true);
					webview.getSettings().setUseWideViewPort(true);
					webview.setWebChromeClient(new DefaultWebChromeClient());
					webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
					webview.getSettings().setLoadWithOverviewMode(true);
					webview.loadUrl(data);
				}else if(a == 4){
					String data = contentlist4.get(arg2);
					webview.loadDataWithBaseURL(null, data, null, "utf-8", null);
				}else{
					String data = contentlist1.get(arg2);
					webview.loadDataWithBaseURL(null, data, null, "utf-8", null);
				}
			}
		});
	}
	
	private class DefaultWebChromeClient extends WebChromeClient {
		// 一个回调接口使用的主机应用程序通知当前页面的自定义视图已被撤职

		// 进入全屏的时候
		@Override
		public void onShowCustomView(View view, CustomViewCallback callback) {
			// 将video放到当前视图中
			bofang.addView(view);
			// 横屏显示
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			// 设置全屏
			setFullScreen();
		}

		// 退出全屏的时候
		@Override
		public void onHideCustomView() {
			// 用户当前的首选方向
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			// 退出全屏
			quitFullScreen();
		}

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			super.onProgressChanged(view, newProgress);
		}
	}
	
	// 关联webview 类
			class MyWebViewClient extends WebViewClient {

				// 加载结束的时候
				@Override
				public void onPageFinished(WebView view, String url) {
					// TODO Auto-generated method stub
					super.onPageFinished(view, url);
				}

			}
	
	/**
	 * 设置全屏
	 */
	private void setFullScreen() {
		// 设置全屏的相关属性，获取当前的屏幕状态，然后设置全屏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		// 全屏下的状态码：1098974464
		// 窗口下的状态吗：1098973440
	}
	
	/**
	 * 退出全屏
	 */
	private void quitFullScreen() {
		// 声明当前屏幕状态的参数并获取
		final WindowManager.LayoutParams attrs = getWindow().getAttributes();
		attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().setAttributes(attrs);
		getWindow()
				.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
	}
	
	class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			if(a == 1){
				return titlelist1.size();
			}else if(a == 2){
				return titlelist2.size();
			}else if(a ==3){
				return titlelist3.size();
			}else if(a == 4){
				return titlelist4.size();
			}else{
				return titlelist1.size();
			}
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View inflate = getLayoutInflater().inflate(R.layout.list_findsearch, null);
			TextView tv_title = (TextView) inflate.findViewById(R.id.tv_findsearch);
			if(a == 1){
				tv_title.setText(titlelist1.get(arg0));
			}else if(a == 2){
				tv_title.setText(titlelist2.get(arg0));
			}else if(a ==3){
				tv_title.setText(titlelist3.get(arg0));
			}else if(a == 4){
				tv_title.setText(titlelist4.get(arg0));
			}else{
				tv_title.setText(titlelist1.get(arg0));
			}
			return inflate;
		}
		
	}
	
	public void getHuodong(){
		if (contactService == null) {
			contactService = new ContactService(getApplicationContext());
		}
		
		contactService.FindSearch(url_huodong, 1, 20, new GetOneRecordListener<List<Huodong>>() {

			@Override
			public void onFinish(List<Huodong> data) {
				titlelist1.clear();
				for(int i=0;i<data.size();i++){
					Huodong huodong = data.get(i);
					String content = huodong.getContent();
					String title = huodong.getTitle();
					String data1 = content.replaceAll("&lt;", "<");
					String data2 = data1.replaceAll("&gt;", ">");
					String data3 = data2.replaceAll("&quot;", "\"");
					String data4 = data3.replaceAll("&amp;", "&");
					String data5 = data4.replaceAll("&nbsp;", " ");
					titlelist1.add(title);
					contentlist1.add(data5);
				}
				isok1 = true;
			}

			@Override
			public void onFailed(String Message) {
				
			}
		});
	}
	
	public void getXinwen(){
		if (contactService == null) {
			contactService = new ContactService(getApplicationContext());
		}
		
		contactService.FindSearch(url_xinwen, 1, 20, new GetOneRecordListener<List<Huodong>>() {

			@Override
			public void onFinish(List<Huodong> data) {
				titlelist2.clear();
				for(int i=0;i<data.size();i++){
					Huodong huodong = data.get(i);
					String content = huodong.getContent();
					String title = huodong.getTitle();
					String data1 = content.replaceAll("&lt;", "<");
					String data2 = data1.replaceAll("&gt;", ">");
					String data3 = data2.replaceAll("&quot;", "\"");
					String data4 = data3.replaceAll("&amp;", "&");
					String data5 = data4.replaceAll("&nbsp;", " ");
					titlelist2.add(title);
					contentlist2.add(data5);
				}
				isok2 = true;
			}

			@Override
			public void onFailed(String Message) {
				
			}
		});
	}
	
	public void getShiping(){
		if (contactService == null) {
			contactService = new ContactService(getApplicationContext());
		}
		
		contactService.FindSearch(url_shiping, 1, 20, new GetOneRecordListener<List<Huodong>>() {

			@Override
			public void onFinish(List<Huodong> data) {
				titlelist3.clear();
				for(int i=0;i<data.size();i++){
					Huodong huodong = data.get(i);
					String content = huodong.getContent();
					String title = huodong.getTitle();
					String data1 = content.replaceAll("&lt;", "<");
					String data2 = data1.replaceAll("&gt;", ">");
					String data3 = data2.replaceAll("&quot;", "\"");
					String data4 = data3.replaceAll("&amp;", "&");
					String data5 = data4.replaceAll("&nbsp;", " ");
					titlelist3.add(title);
					contentlist3.add(data5);
				}
				isok3 = true;
			}

			@Override
			public void onFailed(String Message) {
				
			}
		});
	}
	
	public void getZhuanjia(){
		if (contactService == null) {
			contactService = new ContactService(getApplicationContext());
		}
		
		contactService.FindSearch(url_zhuanjia, 1, 20, new GetOneRecordListener<List<Huodong>>() {

			@Override
			public void onFinish(List<Huodong> data) {
				titlelist4.clear();
				for(int i=0;i<data.size();i++){
					Huodong huodong = data.get(i);
					String content = huodong.getContent();
					String title = huodong.getTitle();
					String data1 = content.replaceAll("&lt;", "<");
					String data2 = data1.replaceAll("&gt;", ">");
					String data3 = data2.replaceAll("&quot;", "\"");
					String data4 = data3.replaceAll("&amp;", "&");
					String data5 = data4.replaceAll("&nbsp;", " ");
					titlelist4.add(title);
					contentlist4.add(data5);
				}
				isok4 = true;
			}

			@Override
			public void onFailed(String Message) {
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.find_search, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lay_back_find:
			finish();
			break;
		case R.id.lay_searhuodong:
			huodong();
			break;
		case R.id.lay_searxinwen:
			xinwen();		
			break;
		case R.id.lay_searshiping:
			shiping();
			break;
		case R.id.lay_searzhuanjia:
			zhuanjia();
			break;
		default:
			break;
		}
	}

	private void zhuanjia() {
		webview.reload();
		tv_hudong.setTextColor(Color.parseColor("#bfbfbf"));
		tv_xinwen.setTextColor(Color.parseColor("#bfbfbf"));
		tv_shiping.setTextColor(Color.parseColor("#bfbfbf"));
		tv_zhuanjia.setTextColor(Color.parseColor("#52B535"));
		imag_huodong.setImageResource(R.drawable.huodong_unselect);
		imag_xinwen.setImageResource(R.drawable.xinwen_unselect);
		imag_shiping.setImageResource(R.drawable.shipin_unselect);
		imag_zhuangjia.setImageResource(R.drawable.zhuanjia_select);
		a = 4;
		if(isfirst1 = true){
			listview.setVisibility(View.VISIBLE);
			webview.setVisibility(View.GONE);
			initListview();
			adapter.notifyDataSetChanged();
		}else{
			listview.setVisibility(View.GONE);
			webview.setVisibility(View.VISIBLE);
		}
	}

	private void shiping() {
		webview.reload();
		tv_hudong.setTextColor(Color.parseColor("#bfbfbf"));
		tv_xinwen.setTextColor(Color.parseColor("#bfbfbf"));
		tv_shiping.setTextColor(Color.parseColor("#52B535"));
		tv_zhuanjia.setTextColor(Color.parseColor("#bfbfbf"));
		imag_huodong.setImageResource(R.drawable.huodong_unselect);
		imag_xinwen.setImageResource(R.drawable.xinwen_unselect);
		imag_shiping.setImageResource(R.drawable.shipin_select);
		imag_zhuangjia.setImageResource(R.drawable.zhuanjia_unselect);
		a = 3;
		if(isfirst1 = true){
			listview.setVisibility(View.VISIBLE);
			webview.setVisibility(View.GONE);
			initListview();
			adapter.notifyDataSetChanged();
		}else{
			listview.setVisibility(View.GONE);
			webview.setVisibility(View.VISIBLE);
		}
	}

	private void xinwen() {
		webview.reload();
		tv_hudong.setTextColor(Color.parseColor("#bfbfbf"));
		tv_xinwen.setTextColor(Color.parseColor("#52B535"));
		tv_shiping.setTextColor(Color.parseColor("#bfbfbf"));
		tv_zhuanjia.setTextColor(Color.parseColor("#bfbfbf"));
		imag_huodong.setImageResource(R.drawable.huodong_unselect);
		imag_xinwen.setImageResource(R.drawable.xinwen_select);
		imag_shiping.setImageResource(R.drawable.shipin_unselect);
		imag_zhuangjia.setImageResource(R.drawable.zhuanjia_unselect);
		a = 2;
		if(isfirst1 = true){
			listview.setVisibility(View.VISIBLE);
			webview.setVisibility(View.GONE);
			initListview();
			adapter.notifyDataSetChanged();
		}else{
			listview.setVisibility(View.GONE);
			webview.setVisibility(View.VISIBLE);
		}
	}

	private void huodong() {
		webview.reload();
		tv_hudong.setTextColor(Color.parseColor("#52B535"));
		tv_xinwen.setTextColor(Color.parseColor("#bfbfbf"));
		tv_shiping.setTextColor(Color.parseColor("#bfbfbf"));
		tv_zhuanjia.setTextColor(Color.parseColor("#bfbfbf"));
		imag_huodong.setImageResource(R.drawable.huodong_select);
		imag_xinwen.setImageResource(R.drawable.xinwen_unselect);
		imag_shiping.setImageResource(R.drawable.shipin_unselect);
		imag_zhuangjia.setImageResource(R.drawable.zhuanjia_unselect);
		a = 1;
		if(isfirst1 = true){
			listview.setVisibility(View.VISIBLE);
			webview.setVisibility(View.GONE);
			initListview();
			adapter.notifyDataSetChanged();
		}else{
			listview.setVisibility(View.GONE);
			webview.setVisibility(View.VISIBLE);
		}
	}

}
